/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddUser.java
 *
 * Created on Nov 7, 2010, 11:51:15 PM
 */

package lmclogistics;

import components.Text;
import components.TextArea;
import components.Image;
import components.NButton;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JPasswordField;
import sql.Sql;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Sonia Tan
 */
public class AddUser extends javax.swing.JFrame {

    /** Creates new form Trucking */

    TextArea usernameField = new TextArea();
    JPasswordField passwordField = new JPasswordField();
    JComboBox access;


    private Image bg;

    public AddUser() {

        setTitle("Add User");
        setAlwaysOnTop(true);
        setSize(300, 200);
        centerScreen();
        setVisible(true);
        setBg();
        okCancelButtons();
        initText();
        initFlds();
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLayout(null);
        comboBox();

        BufferedImage image = null;
        try {
            image = ImageIO.read(getClass().getResource("/images/favicon.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        setIconImage(image);
    }

    public void setBg(){
        bg = new Image("panel.png",507,460);
        add(bg,-1);

    }

    public int getCenterX(int x){

        int a = 919/2 - x/2;


        return a;
    }

    public int getCenterY(int x){

        int a = 577/2 - x/2;


        return a;
    }

    public void okCancelButtons()
    {
                NButton cancel = new NButton("Cancel",200,130);
                NButton ok = new NButton("Ok",115 ,130);
                add(ok,0);
                add(cancel,0);
                cancel.addActionListener(new ActionListener(){

                public void actionPerformed(ActionEvent e) {
                    dispose();
                    }
                });

                ok.addActionListener(new ActionListener(){

                public void actionPerformed(ActionEvent e) {
                    //FormWarning warn = new FormWarning("incorrect");
                    addUser();
                    }
                });
    }

    public void addUser()
    {
        try {
                String query = "INSERT INTO user(userName, userPassword, userAccess) " +
                " VALUES('"+usernameField.getText()+"','"+passwordField.getText()+"','"+access.getSelectedIndex()+"')";


                Connection connection = Sql.getSqlConnection();
                Statement stmt = connection.createStatement();
                stmt.executeUpdate(query);
                Sql.closeSqlConnection(connection);

                } catch (Exception ex) {
                Logger.getLogger(AddUser.class.getName()).log(Level.SEVERE, null, ex);
                }
                this.dispose();

    }

    public void initText(){
        Text username = new Text();
        Text password = new Text();
        Text access = new Text();


        username.inText("Username:", 15, 15);
        password.inText("Password:",15,40);
        access.inText("Access Level:", 15, 65);

        add(username,0);
        add(password,0);
        add(access,0);

    }

    private void initFlds() {

        usernameField.initText(60, 25, 100, 13);
        usernameField.setSize(150,25);
        passwordField.setSize(150, 25);
        passwordField.setLocation(100,38);


        add(usernameField,0);
        add(passwordField,0);

    }


    public void centerScreen(){

    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

    // Determine the new location of the window
    int w = getSize().width;
    int h = getSize().height;
    int x = (dim.width-w)/2;
    int y = (dim.height-h)/2;

    // Move the window
    setLocation(x, y);
    }

    public void comboBox(){

        access = new JComboBox();

        DefaultComboBoxModel model;

        String[] accessLevel = new String[] {"Administrator", "Inventory","Billing"};

        model = new DefaultComboBoxModel(accessLevel);

        access.setModel(model);

        access.setLocation(113, 63);
        access.setSize(120,25);
        add(access,0);

    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
